<?php


namespace Api\Controller;


/** 
 * 与我相关
 * @author Legend. <xcx_legender@qq.com>
 * @param 
 * @return 
 */

class AtmeController extends UserBaseController 
{

	// 与我相关的信息
	public function index()
	{
		//start modify by susy.liang<liangling@ilongyuan.com.cn>
		/*$timestamp = I('timestamp',0, 'intval');
		$pager 	   = I('pager', C('HOME_VIDEO_PER',NULL, 5), 'intval');
		$messEvent = A('Api/Message', 'Event');
		$map['to'] = UID;
		$map['create_time'] = $timestamp ? array('lt', $timestamp) : array('gt', 0);
		$video_ids = $messEvent->getTargetList($map, $pager);

		$likeEvent = A('Api/Like', 'Event');
		$comEvent  = A('Api/Comments', 'Event');
		$vEvent    = A('Api/Video', 'Event');
		foreach ($video_ids as $key => $video_id) {
			$data['video']    = $vEvent->detail($video_id);
			$data['comments'] = $comEvent->get($video_id, 5);
			$data['comments'] = $data['comments'] ? $data['comments'] : array();
			$data['zan']      = $likeEvent->getUser($video_id,5);
			$data['zan']      = $data['zan'] ? $data['zan'] : array();
			$list[] = $data;
		}

		$this->success('', $list);*/

		/*$messEvent = A('Api/Message', 'Event');
		//$likeEvent = A('Api/Like', 'Event');
		$comEvent  = A('Api/Comments', 'Event');
		$vEvent    = A('Api/Video', 'Event');

		$map = array();
		$list = array();
		$map['to'] = UID;
		$map['type'] = ['neq', 'zan'];
		$timestamp = I('timestamp',0, 'intval');
		$map['create_time'] = $timestamp ? array('lt', $timestamp) : array('gt', 0);
		//$pager 	   = C('USER_LIST_NUM',NULL, 20);
		$pager 	   = I('pager', C('USER_LIST_NUM',NULL, 20), 'intval');

		$resData = $messEvent->getMsgList($map, $pager);

		if($resData){
 			foreach ($resData['ids'] as $key => $videoId) {
				$video = $vEvent->detail($videoId, UID, false);
				$data['video']    = $video;
				
				if($video['uid'] == UID){
					$data['zan']      = $likeEvent->getUser($videoId,5);
					$data['zan']      = $data['zan'] ? $data['zan'] : array();
					//($vid, $limit = '', $field='', $filter=array(), $timeFormat=false, $getTime=false)
					$data['comments'] = $comEvent->get($videoId, 5);
					$data['comments'] = $data['comments'] ? $data['comments'] : array();
				}else{

					$query = array();
					$cmtMap = array();
					$where = array();

					$query[]['uid']     = array('$eq'=>intval(UID));
					$query[]['referrer'] =  array('$eq'=>intval(UID));
					$cmtMap['$and'][]['$or'] = $query;
					$cmtMap['$and'][]['video_id'] = intval($videoId);
					$where['_array'] = $cmtMap;
					
					$data['comments'] = $comEvent->get(0, 5, '', $where);

				}

				$data['message_time'] = $resData['timestamp'];				
				$list[] = $data;
			}

		}*/

		// $msgMap = array('target_id'=>array('in',$resData['ids']));
		// $messEvent->setRead('', $msgMap);
		// 
		
		$messEvent = A('Api/Message', 'Event');
		$pager 	   = I('pager', C('USER_LIST_NUM',NULL, 20), 'intval');


		$map = array();
		$map['to'] = UID;
		$map['type'] = ['eq', 'at'];
		$map['object_id'] = ['neq', 0];
		$timestamp = I('timestamp',0, 'intval');
		$map['create_time'] = $timestamp ? array('lt', $timestamp) : array('gt', 0);

		$res = $messEvent->getMsgList($map, $pager);

		$lists = [];

		$vEvent = event('Video');
		$cmtEvent = event('Comments');

		foreach ($res as $item) {
			$data = [];
			$data['video'] = $vEvent->detail($item['target_id']);
			if (!empty($item['object_id'])){
				$data['comments'][] = $cmtEvent->getOne($item['object_id']);
			}else{
				$data['comments'] = $cmtEvent->get($item['target_id'], 5);
			}
			$data['message_time'] = $data['comments'][0]['create_time'];
			$lists[] = $data;
		}

		// 修改类型为at的数据为已读
		$messEvent->setRead(UID, ['type' => 'at']);

		$this->success('', $lists);

		//end
	}





}